//
//  HTCityViewController.m
//  HeartTravel
//
//  Created by lanou on 15/11/2.
//  Copyright © 2015年 陈文杰. All rights reserved.
//

#import "HTCityViewController.h"
#import "HTCityDetailTableViewCell.h"
#import "PKRequestManager.h"
#import "HTDetailViewController.h"
#import "HTCityDetailModel.h"
#define kWidth SCREEN_WIDTH/5
@interface HTCityViewController ()<UITableViewDataSource, UITableViewDelegate>
@property (nonatomic, strong) UIView *toppView;;
@property (nonatomic, strong) UITableView *mainListTableView;
@property (nonatomic, strong) UIView *blueView;
@property (nonatomic, strong) NSMutableArray *dataSourceArray;
@end

@implementation HTCityViewController
- (NSMutableArray *)dataSourceArray{
    if (!_dataSourceArray) {
        _dataSourceArray = [NSMutableArray arrayWithCapacity:0];
    }
    return _dataSourceArray;
}

- (void)requestSightDataSource:(NSString *)urlString{
    [PKRequestManager requestWithType:GET urlString:urlString parDic:nil finish:^(NSData *data) {
        NSDictionary *dict = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers | NSJSONReadingMutableLeaves error:nil];
        for (NSDictionary *dic in dict[@"items"]) {
            HTCityDetailModel *model = [[HTCityDetailModel alloc]init];
            [model setValuesForKeysWithDictionary:dic];
            [self.dataSourceArray addObject:model];
        }
        [self.mainListTableView reloadData];
    } error:^(NSError *error) {
        NSLog(@"城市风景栏数据请求失败%@", error);
    }];
}



- (void)viewDidLoad {
    [super viewDidLoad];
    NSString *urlString = [@"http://api.breadtrip.com/destination/place" stringByAppendingFormat:@"%@%@", self.urlString, @"pois/sights/?sort=default&start=0"];
    [self requestSightDataSource:urlString];
    
    self.topView.backgroundColor = [UIColor colorWithRed:0.351 green:0.359 blue:0.350 alpha:1.000];
    [self.leftButton setBackgroundImage:[UIImage imageNamed:@"返回"] forState:(UIControlStateNormal)];
    [self.leftButton addTarget:self action:@selector(didClickBackLastViewController) forControlEvents:(UIControlEventTouchUpInside)];
    UILabel *titleLabel = [[UILabel alloc]initWithFrame:CGRectMake(38, 15, 150, 20)];
    titleLabel.textColor = [UIColor whiteColor];
    titleLabel.text = self.titleName;
    [self.topView addSubview:titleLabel];
    [self addScrollView];
    
    self.mainListTableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 115, SCREEN_WIDTH, SCREEN_HEIGHT - 115)];
    self.mainListTableView.dataSource = self;
    self.mainListTableView.delegate = self;
    [_mainListTableView registerClass:[HTCityDetailTableViewCell class] forCellReuseIdentifier:@"HTCityDetailTableViewCell"];
    self.mainListTableView.rowHeight = SCREEN_WIDTH/3 + 30;
    [self.view addSubview:_mainListTableView];
}

- (void)didClickLookSecnic{
    [self.dataSourceArray removeAllObjects];
    _blueView.frame = CGRectMake(0, 42, kWidth, 3);
    NSString *urlString = [@"http://api.breadtrip.com/destination/place" stringByAppendingFormat:@"%@%@", self.urlString, @"pois/sights/?sort=default&start=0"];
    [self requestSightDataSource:urlString];
}

- (void)didClickLookHotel{
    [self.dataSourceArray removeAllObjects];
    _blueView.frame = CGRectMake(kWidth, 42, kWidth, 3);
    NSString *urlString = [@"http://api.breadtrip.com/destination/place" stringByAppendingFormat:@"%@%@", self.urlString, @"pois/hotel/?sort=default&start=0"];
    [self requestSightDataSource:urlString];
}

- (void)didClickLookEat{
    [self.dataSourceArray removeAllObjects];
    _blueView.frame = CGRectMake(kWidth * 2, 42, kWidth, 3);
    NSString *urlString = [@"http://api.breadtrip.com/destination/place" stringByAppendingFormat:@"%@%@", self.urlString, @"pois/restaurant/?sort=default&start=0"];
    [self requestSightDataSource:urlString];
}

- (void)didClickLookShop{
    [self.dataSourceArray removeAllObjects];
    _blueView.frame = CGRectMake(kWidth * 3, 42, kWidth, 3);
    NSString *urlString = [@"http://api.breadtrip.com/destination/place" stringByAppendingFormat:@"%@%@", self.urlString, @"pois/mall/?sort=default&start=0"];
    [self requestSightDataSource:urlString];
}

- (void)didClickLookPlay{
    [self.dataSourceArray removeAllObjects];
    _blueView.frame = CGRectMake(kWidth * 4, 42, kWidth, 3);
    NSString *urlString = [@"http://api.breadtrip.com/destination/place" stringByAppendingFormat:@"%@%@", self.urlString, @"pois/experience/?sort=default&start=0"];
    [self requestSightDataSource:urlString];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.dataSourceArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    HTCityDetailTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"HTCityDetailTableViewCell" forIndexPath:indexPath];
    cell.configureModel = self.dataSourceArray[indexPath.row];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    return cell;
}

- (void)addScrollView{
    self.toppView = [[UIView alloc]initWithFrame:CGRectMake(0, 70, SCREEN_WIDTH, 45)];
    _toppView.backgroundColor = [UIColor colorWithRed:0.683 green:1.000 blue:0.988 alpha:1.000];
    UIButton *button1 = [UIButton buttonWithType:(UIButtonTypeCustom)];
    button1.frame = CGRectMake(0, 0, kWidth, 45);
    [button1 setTitle:@"风景" forState:(UIControlStateNormal)];
    [button1 setTitleColor:[UIColor blackColor] forState:(UIControlStateNormal)];
    [button1 addTarget:self action:@selector(didClickLookSecnic) forControlEvents:(UIControlEventTouchUpInside)];
    UIButton *button2 = [UIButton buttonWithType:(UIButtonTypeCustom)];
    button2.frame = CGRectMake(kWidth, 0, kWidth, 45);
    [button2 setTitle:@"住宿" forState:(UIControlStateNormal)];
    [button2 setTitleColor:[UIColor blackColor] forState:(UIControlStateNormal)];
    [button2 addTarget:self action:@selector(didClickLookHotel) forControlEvents:(UIControlEventTouchUpInside)];
    UIButton *button3 = [UIButton buttonWithType:(UIButtonTypeCustom)];
    button3.frame = CGRectMake(2*kWidth, 0, kWidth, 45);
    [button3 setTitle:@"餐厅" forState:(UIControlStateNormal)];
    [button3 setTitleColor:[UIColor blackColor] forState:(UIControlStateNormal)];
    [button3 addTarget:self action:@selector(didClickLookEat) forControlEvents:(UIControlEventTouchUpInside)];
    UIButton *button4 = [UIButton buttonWithType:(UIButtonTypeCustom)];
    button4.frame = CGRectMake(3*kWidth, 0, kWidth, 45);
    [button4 setTitle:@"购物" forState:(UIControlStateNormal)];
    [button4 setTitleColor:[UIColor blackColor] forState:(UIControlStateNormal)];
    [button4 addTarget:self action:@selector(didClickLookShop) forControlEvents:(UIControlEventTouchUpInside)];
    UIButton *button5 = [UIButton buttonWithType:(UIButtonTypeCustom)];
    button5.frame = CGRectMake(4*kWidth, 0, kWidth, 45);
    [button5 setTitle:@"娱乐" forState:(UIControlStateNormal)];
    [button5 setTitleColor:[UIColor blackColor] forState:(UIControlStateNormal)];
    [button5 addTarget:self action:@selector(didClickLookPlay) forControlEvents:(UIControlEventTouchUpInside)];
    _blueView = [[UIView alloc]init];
    _blueView.backgroundColor = [UIColor blueColor];
    _blueView.frame = CGRectMake(0, 42, kWidth, 3);
    
    [self.view addSubview:_toppView];
    [self.toppView addSubview:button1];
    [self.toppView addSubview:button2];
    [self.toppView addSubview:button3];
    [self.toppView addSubview:button4];
    [self.toppView addSubview:button5];
    [self.toppView addSubview:_blueView];
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    HTDetailViewController *detailVC = [[HTDetailViewController alloc]init];
    HTCityDetailModel *model = self.dataSourceArray[indexPath.row];
    detailVC.detailModel = model;
    [self.navigationController pushViewController:detailVC animated:nil];
}

- (void)didClickBackLastViewController{
    [self.navigationController popViewControllerAnimated:nil];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
